<?php
namespace app\common\service\setting;
use app\common\cache\setting\SettingCollectionCache;
use app\common\model\setting\SettingCollectionModel;

/**
 * 收款配置
 */
class SettingCollectionService
{
    /**
     * 添加、修改字段
     * @var array
     */
    public static $edit_field = [
            'id' => '',
        'name' => '',
        'account' => '',
        'is_disable' => '',
        'code' => '',
        'ali_appid' => '',
        'ali_app_key' => '',
        'ali_app_publi_key' => '',
        'ali_key' => '',
        'ali_merchant_id' => '',
        'ali_callback_url' => '',
        'image_id' => '',
        'card_no' => '',
        'card_bank' => '',
        'pay_remark' => '',
        'sort' => '',
        'remark' => '',
        'is_default' => '',
    ];
    /**
     * 收款配置列表
     *
     * @param array  $where 条件
     * @param int    $page  页数
     * @param int    $limit 数量
     * @param array  $order 排序
     * @param string $field 字段
     *
     * @return array
     */
    public static function list($where = [], $page = 1, $limit = 10,  $order = [], $field = '')
    {
        $model = new SettingCollectionModel();
        $pk = $model->getPk();
        if (empty($field)) {
            $field = 'id,name,account,is_disable,create_time,update_time,code,ali_appid,ali_app_key,ali_app_publi_key,ali_key,ali_merchant_id,ali_callback_url,image_id,card_bank,pay_remark,sort,remark,is_default';
        }
        if (empty($order)) {
            $order = ['sort'=>'asc',$pk => 'desc'];
        }
        $with = $append = $hidden = [];
        if (strpos($field, 'image_id') !== false) {
            $with[]   = $hidden[] = 'image';
            $append[] = 'image_url';
        }
        if ($page == 0 || $limit == 0) {
            return $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->order($order)->select()->toArray();
        }
        $count = $model->where($where)->count();
        $pages = ceil($count / $limit);
        $list = $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->page($page)->limit($limit)->order($order)->select()->toArray();
        return compact('count', 'pages', 'page', 'limit', 'list');
    }
    /**
     * 收款配置信息
     *
     * @param int  $id   收款配置id
     * @param bool $exce 不存在是否抛出异常
     *
     * @return array|Exception
     */
    public static function info($id, $exce = true)
    {
        $info = SettingCollectionCache::get($id);
        if (empty($info)) {
            $model = new SettingCollectionModel();
            $with =  $hidden = ['image'];
            $append =['image_url'];
            $info = $model->with($with)->append($append)->hidden($hidden)->find($id);
            if (empty($info)) {
                if ($exce) {
                    exception('收款配置不存在：' . $id);
                }
                return [];
            }
            $info = $info->toArray();
            SettingCollectionCache::set($id, $info);
        }
        return $info;
    }
    /**
     * 收款配置添加
     *
     * @param array $param 收款配置信息
     *
     * @return array|Exception
     */
    public static function add($param)
    {
        $model = new SettingCollectionModel();
        $pk = $model->getPk();
        unset($param[$pk]);
        if(isset($param['is_disable']) && $param['is_disable'] == 0){
            $edit = $model->where('code',$param['code'])->update(['is_disable'=>1]);
        }
        $param['create_uid']  = user_id();
        $param['create_time'] = datetime();
        $model->save($param);
        $id = $model->$pk;
        if (empty($id)) {
            exception();
        }
        $param[$pk] = $id;
        return $param;
    }
     /**
     * 收款配置修改
     *
     * @param int|array $ids   收款配置id
     * @param array     $param 收款配置信息
     *
     * @return array|Exception
     */
    public static function edit($ids, $param = [])
    {
        $model = new SettingCollectionModel();
        $pk = $model->getPk();
        unset($param[$pk], $param['ids']);
        $param['update_uid']  = user_id();
        $param['update_time'] = datetime();
        //处理状态
        if(isset($param['is_disable']) && $param['is_disable'] == 0){
            if(count($ids)>0){
                $open_id = $ids[0];
                $edit = $model->where('code',$param['code'])->whereNotIn($pk,array_shift($ids))->update(['is_disable'=>1]);
                $edit = $model->where('code',$param['code'])->where($pk,$open_id)->update(['is_disable'=>0]);
            }else{
                $edit = $model->where('code',$param['code'])->whereNotIn($pk,$ids)->update(['is_disable'=>1]);
            }
            unset($param['is_disable']);
        }
        $res = $model->where($pk, 'in', $ids)->update($param);
//        if (empty($res)) {
//            exception();
//        }
        $param['ids'] = $ids;
        SettingCollectionCache::del($ids);
        return $param;
    }
    /**
     * 收款配置删除
     *
     * @param array $ids  收款配置id
     * @param bool  $real 是否真实删除
     *
     * @return array|Exception
     */
    public static function dele($ids, $real = false)
    {
        $model = new SettingCollectionModel();
        $pk = $model->getPk();
        if ($real) {
            $res = $model->where($pk, 'in', $ids)->delete();
        } else {
            $update = delete_update();
            $res = $model->where($pk, 'in', $ids)->update($update);
        }
        if (empty($res)) {
            exception();
        }
        $update['ids'] = $ids;
        SettingCollectionCache::del($ids);
        return $update;
    }

    /**
     * 查询支付方式
     * @Author: 易军辉
     * @DateTime:2024-07-10 18:08
     * @return array[]
     */
    public static function getPayTypes()
    {
        return SettingCollectionModel::PAY_TYPES;
    }

    /**
     * 查询充值页面所需参数
     * @Author: 易军辉
     * @DateTime:2024-07-10 20:22
     * @return array[]
     */
    public static function getParams()
    {
        //查询支付参数配置
        $where[] = ['is_disable','=',0];
        $where[] = ['is_delete','=',0];
        $list = self::list($where, 0, 0, [],'id,name,account,code,image_id,card_bank,pay_remark');
        $data = [];
        foreach ($list as $key=>$val){
            $type = SettingCollectionModel::getPayType($val['code'],4);
            $data[$val['code']] = $type;
            if($val['code'] =='OnLineAlipay'){
                $data[$val['code']]['item'] =['id'=>$val['id']];
            }else{
                $data[$val['code']]['item'] = $val;
            }
        }
        //排序组装
        $pay_types = SettingCollectionModel::PAY_TYPES;
        $pay_type_list = [];
        foreach ($pay_types as  $k=>$v){
            if(isset($data[$v['code']])){
                array_push($pay_type_list,$data[$v['code']]);
            }
        }
        return compact('pay_type_list');
    }
}
